<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Card extends Common
{
    // 会员权益
    public function power()
    {
        if (request()->isPost()) {
            $time = input('post.time');
            $key = input('post.key');
            $where = array();
            if ($key) {
                $where['title'] = ['like', '%' . $key . '%'];
            }
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('vip_equity')
                ->where($where)
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('VipEquity')->listAddTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    public function powerEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('VipEquity')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('VipEquity')->where(['id' => $id])->find();
            return $this->fetch('', ['list' => $list]);
        }
    }
    public function powerAdd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            return model('VipEquity')->commonData($data, '添加成功', '添加失败，请重试');
        } else {
            return $this->fetch('');
        }
    }
    public function powerDel()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('VipEquity')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
    public function powerStatus()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('VipEquity')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');
        }
    }
    // 会员卡列表
    public function index()
    {
        if (request()->isPost()) {
            $time = input('post.time');
            $key = input('post.key');
            $where = array();
            if ($key) {
                $where['title'] = ['like', '%' . $key . '%'];
            }
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('vip_cate')
                ->where($where)
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('VipCate')->listAddTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            // 会员添加前等级重复验证
            $list = db('vip_cate')
                ->where(['level'=>$data['level']])
                ->find();
            if(isset($list)){
                return $result = ['code'=>8888,'msg'=>'添加失败，会员等级重复'];
            }
            return model('VipCate')->commonData($data, '添加成功', '添加失败，请重试');
        } else {
            $list_category = db('VipEquity')->where(['status' => 0])->select();
            return $this->fetch('', ['list_category' => $list_category]);
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $list = db('vip_cate')
                ->where(['id'=>['<>',$data['id']],'level'=>$data['level']])
                ->find();
            if(isset($list)){
                return $result = ['code'=>8888,'msg'=>'添加失败，会员等级重复'];
            }
            return model('VipCate')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            //获取当前数据
            $list = db('vip_cate')->where(['id' => $id])->find();
            // 获取列表
            $list_category = db('vip_equity')->select();
            // 获取所有勾选的support_id
            $cate_id = explode(',', $list['cate_id']);
            foreach ($cate_id as $key => $item) {
                foreach ($list_category as $index => $vo) {
                    if ($item == $vo['id']) {
                        $list_category[$index]['sel'] = true;
                    } else {
                        if (!isset($vo['sel'])) {
                            $list_category[$index]['sel'] = false;
                        }
                    }
                }
            }
            return $this->fetch('', ['list' => $list, 'list_category' => $list_category]);
        }
    }
    public function del()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            
            return model('VipCate')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
    
     public function status()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('VipCate')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');
        }
    }
    // 会员卡等级订单管理
    public function order()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $phone = input('post.phone');
            $status = input('post.status');
            $upgrede_number = input('post.upgrede_number');
            $where = array();
            if ($phone) {
                $where['u.phone'] = $phone;
            }
            if ($status) {
                $where['v.status'] = $status;
            }
            if($upgrede_number){
                $where['v.upgrade_number'] = $upgrede_number;
            }
            $page = input('page') ? input('page') : 1;
            // dump($data);die();
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('vip_upgrade')
                ->alias('v')
                ->join('user u', 'v.user_id = u.user_id')
                ->join('vip_cate c', 'v.new_level = c.level')
                ->where($where)
                ->field('u.realname as realname,v.id as id,v.upgrade_number as upgrade_number,u.phone as phone,u.vip_number as vip_number,c.title as title,v.money as money,v.status as status,v.addtime as addtime')
                ->order('v.id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $i => $item) {
                $list['data'][$i]['addtime'] = date('Y-m-d h:i:s', $item['addtime']);
            }
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'],'count'=>$list['total'], 'rel' => 1];
        } else {
            return $this->fetch('');
        }
    }
    // 会员卡详情查看
    public function orderEdit()
    {
        $id = input('id');
        $list = db('vip_upgrade')
            ->alias('v')
            ->join('user u', 'v.user_id = u.user_id')
            ->join('vip_cate c', 'v.new_level = c.level')
            ->join('vip_cate n', 'v.old_level = n.level')
            ->where(['v.id' => $id])
            ->field('v.id as id,u.realname as name,u.phone as phone,u.vip_number as vip_number,v.upgrade_number as upgrade_number,n.title as old_level,c.title as new_level,v.money as money,v.paytime as paytime,v.addtime as addtime,v.status as status,v.remark as remark')
            ->find();
       $list['addtime'] = date('Y-m-d h:i:s', $list['addtime']);
       $list['paytime'] = date('Y-m-d h:i:s', $list['paytime']);
        return $this->fetch('',['list'=>$list]);
    }
    public function orderDel()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('VipUpgrade')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
}
